package hlhj.fhp.checkpoint.wxapi

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.example.mymvp.log
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelpay.PayResp
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import hlhj.fhp.checkpoint.Constant
import hlhj.fhp.checkpoint.R
import hlhj.fhp.checkpoint.javaBean.WxPaySuccessEvent
import org.greenrobot.eventbus.EventBus

/**
 * Created by Never Fear   on 2018\11\26 0026.
Never More....
 */
class WXPayEntryActivity :Activity(),IWXAPIEventHandler {
    private var api:IWXAPI?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.pay_result)

        Log.e("fhp","----------到了回调页面1")
        api = WXAPIFactory.createWXAPI(this, Constant.WXAPPID, true)

        api?.registerApp(Constant.WXAPPID)

        api?.handleIntent(intent, this)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        Log.e("fhp","----------到了回调页面2")
        setIntent(intent)
        api?.handleIntent(intent,this)
    }
    override fun onResp(p0: BaseResp?) {
        log("fhp","请求${p0?.errCode}")
        val payResp = p0 as PayResp

        if (p0?.errCode==0){
            if(payResp?.extData.equals("支付界面")){
                EventBus.getDefault().post(WxPaySuccessEvent())
            }else if (payResp?.extData.equals("激活用户")){
                Constant.isActive=true
                EventBus.getDefault().post(WxPaySuccessEvent())

            }
        }
        finish()
    }

    override fun onReq(p0: BaseReq?) {
        log("fhp","响应${p0?.type}")

    }
}